//
//  CQTableViewCardCollectionViewCell.swift
//  TSListDemo
//
//  Created by qian on 2020/11/16.
//  Copyright © 2020 dvlproad. All rights reserved.
//

import UIKit
import CQProcessKit

class CQTableViewCardCollectionViewCell: CQBaseCardCollectionViewCell {
    var tableView: CardTableView!
    var likeView: UIImageView!
    var hateView: UIImageView!
    var progressView: CJVerticalLineProgressView!
//    fileprivate var lastFrame: CGRect?
    
    // MARK: - Init
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupViews()
        
        likeView.alpha = 0
        hateView.alpha = 0
        progressView.progress = 0
        tableView.didScrollPercent = { (maxYPercentCurrent, maxYPercentWhenMinYisZero) -> () in
            debugPrint("maxY当前滑道的百分比 = \(maxYPercentCurrent), 当minY=0时候，maxY滑道的百分比 = \(maxYPercentWhenMinYisZero)")
            self.progressView.maxYPercentWhenMinYisZero = maxYPercentWhenMinYisZero;
            self.progressView.progress = maxYPercentCurrent
        }
        
    }
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        debugPrint("layoutSubviews CQTableViewCardCollectionViewCell")
//
//        if self.lastFrame != self.frame || self.frame == CGRect.zero {
//            debugPrint("self.lastFrame != self.frame CQTableViewCardCollectionViewCell")
//            self.tableView.tableHeaderViewHeight = self.frame.height
//            self.lastFrame = self.frame
//        }
//    }
    
    // MARK: - SetupViews
    func setupViews() {
        self.layer.cornerRadius = 30
        self.layer.masksToBounds = true
    
        self.tableView = CardTableView.init(frame: CGRect.zero, style: UITableView.Style.grouped)
        
        self.addSubview(self.tableView)
        self.tableView.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        
        hateView = likeHateIcon(named: "icon_hate")
        self.addSubview(hateView)
        hateView.snp_makeConstraints({ (make) in
            make.right.equalTo(self).offset(-40)
            make.width.equalTo(80)
            make.top.equalTo(self).offset(20)
            make.height.equalTo(hateView.snp_width).multipliedBy(1);
        })
        
        likeView = likeHateIcon(named: "icon_like")
        self.addSubview(likeView)
        likeView.snp_makeConstraints({ (make) in
            make.left.equalTo(self).offset(40)
            make.width.equalTo(hateView)
            make.top.equalTo(hateView)
            make.height.equalTo(hateView)
        })
        
        progressView = CQProgressViewFactory.verticalLineProgressView()
        self.addSubview(progressView)
        progressView.snp_makeConstraints({ (make) in
            make.right.equalTo(self).offset(-40)
            make.width.equalTo(10)
            make.top.equalTo(self).offset(40)
            make.height.equalTo(100)
        })
    }
    
    // MARK: - Private Method
    private func likeHateIcon(named: String) -> UIImageView {
        let imageView: UIImageView = UIImageView.init()
        imageView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        imageView.layer.cornerRadius = 40
        imageView.layer.masksToBounds = true
        imageView.image = UIImage.init(named: named)
        imageView.contentMode = UIView.ContentMode.center
        
        return imageView;
    }
}
